//
//  LYMinuteController.m
//  PianoAndPoemDirectSeeding
//
//  Created by 李云 on 2017/7/19.
//  Copyright © 2017年 梅津山. All rights reserved.
//

#import "LYMinuteController.h"
#import "LYReleaController.h"
#import "FinishController.h"
#import <AVOSCloud.h>
#import "HomeModel.h"
#import <MBProgressHUD/MBProgressHUD.h>


#define S_W [UIScreen mainScreen].bounds.size.width //屏幕宽
#define S_H [UIScreen mainScreen].bounds.size.height//高

@interface LYMinuteController ()<UIScrollViewDelegate>
@property (nonatomic,strong) UIScrollView *scrollView;
@property (weak, nonatomic) IBOutlet UIButton *releaseBtn;
@property (weak, nonatomic) IBOutlet UIButton *finishBtn;
@property (nonatomic, strong) NSArray *releaseArr;
@property (nonatomic, strong) NSArray *finishArr;


@end

@implementation LYMinuteController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.scrollView];
    //滚动范围
    self.scrollView.contentSize = CGSizeMake(S_W*2, 0);
    [self dataCliced];
}
- (IBAction)back:(UIButton *)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}


- (IBAction)release:(UIButton *)sender {
    sender .selected = YES;
    [self.scrollView setContentOffset:CGPointMake(0,-64)];
    
    _releaseBtn.selected = NO;
    
}
- (IBAction)finshBtn:(UIButton *)sender {
    sender .selected = YES;
    [self.scrollView setContentOffset:CGPointMake(375,-64)];
    
    _finishBtn.selected = NO;
}

-(UIScrollView *)scrollView{
    if (!_scrollView) {
        _scrollView =[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, S_W, S_H)];
      //  _scrollView.backgroundColor =[UIColor redColor];
        _scrollView.pagingEnabled = YES;
        _scrollView.showsHorizontalScrollIndicator = NO;
        _scrollView.bounces = NO;
        _scrollView.delegate = self;
        
    }
    return _scrollView;
}
- (void)lasteWishView{
    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    LYReleaController *vc1 =[sb instantiateViewControllerWithIdentifier:@"LYReleaController"];
    vc1.dataArr = self.releaseArr;
    [self addChildViewController:vc1];
    vc1.tableView.frame = CGRectMake(375, 0, S_W, S_H);
    [self.scrollView addSubview:vc1.tableView];
    
}
-(void)hotWishView{
    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    FinishController *vc =[sb instantiateViewControllerWithIdentifier:@"FinishController"];
    vc.dataArr = self.finishArr;
    [self addChildViewController:vc];
    [self.scrollView addSubview:vc.tableView];
}

//取得当前用户发布的消息
- (void)dataCliced{
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    AVQuery *query = [AVQuery queryWithClassName:@"Content"];
    [query orderByDescending:@"createdAt"];
    // owner 为 Pointer，指向 _User 表
    [query includeKey:@"owner"];
    // image 为 File
    [query includeKey:@"imageArr"];
    [query whereKey:@"owner" equalTo:[AVUser currentUser]];
    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        if (objects) {
            [MBProgressHUD hideHUDForView:self.view animated:YES];
            NSMutableArray *finish = [NSMutableArray array];
            NSMutableArray *release = [NSMutableArray array];
            for (AVObject *objc in objects) {
                NSArray *fileArr = [objc valueForKey:@"imageArr"];
                AVUser *user = [objc objectForKey:@"owner"];
                
                NSString *par = [objc objectForKey:@"particulars"];
                NSString * loca = [objc objectForKey:@"location"];
                NSString *state = [objc objectForKey:@"state"];
                NSString *grade = [objc objectForKey:@"grade"];
                NSDate *createdAt = [objc objectForKey:@"createdAt"];
                NSString *image =  [user objectForKey:@"image"];
                
                NSString *name = user.username;
                //  NSLog(@"%@",[objc objectForKey:@"particulars"]);
                NSMutableArray *urlArr = [NSMutableArray arrayWithCapacity:fileArr.count];
                for (AVFile *file in fileArr) {
                    [urlArr addObject:file.url];
                }
                
                NSDictionary *dict = @{@"owner":user,@"grade":grade,@"state":state,@"particulars":par,@"location":loca,@"createdAt":createdAt,@"imageArr":urlArr,@"image":image,@"name":name};
                
                if ([state isEqualToString:@"未维修"]) {
                    HomeModel *model = [[HomeModel alloc] initWithNewDict:dict];
                    [release addObject:model];
                }else{
                    HomeModel *model = [[HomeModel alloc] initWithNewDict:dict];
                    [finish addObject:model];
                }
            }
            self.finishArr = finish;
            self.releaseArr = release;
            [self lasteWishView];
            [self hotWishView];
        }
    }];
    
}



@end
